Requires Scripting PRO
The HealthQuantitySample class represents a single health quantity data point, such as a heart rate measurement, a recorded step count, or a logged calorie value. It provides information about the measurement’s type, time interval, unit, value, and optional metadata.
This class is the base for more specialized subclasses: HealthCumulativeQuantitySample and HealthDiscreteQuantitySample.
A HealthQuantitySample encapsulates the following:
This class is primarily used for:
| Property | Type | Description |
|---|---|---|
uuid |
string |
Unique identifier for the sample |
quantityType |
HealthQuantityType |
Type of the health metric |
startDate |
Date |
Start of the measurement |
endDate |
Date |
End of the measurement |
count |
number |
Number of samples aggregated (usually 1) |
metadata |
Record<string, any> | null |
Optional metadata dictionary |
quantityValue(unit: HealthUnit): numberConverts the sample's stored value to the specified unit.
Parameters:
unit: A HealthUnit instance (e.g., HealthUnit.kilocalorie())Returns:
Example:
HealthQuantitySample.create(options): HealthQuantitySample | nullCreates a new quantity sample with the specified parameters.
Parameters:
Returns:
HealthQuantitySample if valid, otherwise null.Example:
The HealthCumulativeQuantitySample class represents cumulative data (i.e., totals over time), such as energy burned or distance traveled.
| Property | Type | Description |
|---|---|---|
hasUndeterminedDuration |
boolean |
Indicates whether the sample’s duration is uncertain |
sumQuantity(unit: HealthUnit): numberReturns the total accumulated quantity in the specified unit.
Example:
quantityValue(unit: HealthUnit): numberAlias for sumQuantity() — retrieves the total value in the given unit.
The HealthDiscreteQuantitySample class represents a series of discrete values sampled at specific times — such as heart rate measurements or step counts across a time window.
| Property | Type | Description |
|---|---|---|
mostRecentQuantityDateInterval |
HealthDateInterval | null |
Time window of the most recent recorded value |
| Method | Description |
|---|---|
averageQuantity(unit) |
Returns the average of all values in the specified unit |
maximumQuantity(unit) |
Returns the maximum value in the specified unit |
minimumQuantity(unit) |
Returns the minimum value in the specified unit |
mostRecentQuantity(unit) |
Returns the most recent recorded value (if available) |
| Scenario | Use This Class | Example |
|---|---|---|
| Record or retrieve a single value | HealthQuantitySample |
A manually entered weight |
| Work with totals over time | HealthCumulativeQuantitySample |
Total distance walked in 1 hour |
| Analyze statistics across samples | HealthDiscreteQuantitySample |
Min/Max/Average heart rate during a workout |
HealthUnit: Represents the unit of measurement.HealthQuantityType: Specifies the kind of health metric being measured.HealthDateInterval: A time range used for timestamping specific values.